/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 *
 * @author ccp <568093091@qq.com>
 */
public class HibernateUtil {
       private final static SessionFactory FACTORY = buildSessionFactory();
       
       private static SessionFactory buildSessionFactory(){
           Configuration cfg = new Configuration().configure();
           ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                   applySettings(cfg.getProperties()).buildServiceRegistry();
           SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
           return  sessionFactory;
       }
       
       public static SessionFactory getSessionFactory(){
           return FACTORY;
       }
       
       public static Session openSession(){
           return FACTORY.openSession();
       }
       
       public static void close(Session session){
           if(session!=null) session.close();
       }
       
}
